{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In R (```install.packages(\"Rserve\")```):\n",
    "\n",
    "1. ```library(Rserve); Rserve(args=\"--no-save\")```\n",
    "2. ```source(\"https://bioconductor.org/biocLite.R\")```\n",
    "3. ```biocLite(\"preprocessCore\")```\n",
    "\n",
    "Note:\n",
    "\n",
    "- For a newer R version, install biocLite and preprocessCore using via BiocManager (https://bioconductor.org/install/)\n",
    "- Use R in /usr/bin instead of R in miniconda (this one got gcc problem)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Exit R but keep Rserve running.\n",
    "\n",
    "Note: -Q for no quantile norm (suggested for RNA-seq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# template = \"java -Xmx3g -Xms3g -jar cibersort/CIBERSORT.jar -M preprocessed_data/scRNAseq/{}_sample_avg.tsv -B data/CIBERSORT_related/{}/sig_file_hugo.txt -Q > result/CIBERSORT_deconvolution/{}_{}_decon_result.tsv\"\n",
    "template = \"java -Xmx8g -Xms8g -jar CIBERSORT.jar -M {}_sample_avg_log2tpm.tsv -B sig_file_hugo.txt -Q > {}_{}_log2tpm_decon_result.tsv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "java -Xmx8g -Xms8g -jar CIBERSORT.jar -M AS_et_al_sample_avg_log2tpm.tsv -B sig_file_hugo.txt -Q > AS_et_al_GDSC_subtype_panal_log2tpm_decon_result.tsv\n",
      "java -Xmx8g -Xms8g -jar CIBERSORT.jar -M AP_et_al_sample_avg_log2tpm.tsv -B sig_file_hugo.txt -Q > AP_et_al_GDSC_subtype_panal_log2tpm_decon_result.tsv\n",
      "java -Xmx8g -Xms8g -jar CIBERSORT.jar -M SP_et_al_sample_avg_log2tpm.tsv -B sig_file_hugo.txt -Q > SP_et_al_GDSC_subtype_panal_log2tpm_decon_result.tsv\n",
      "java -Xmx8g -Xms8g -jar CIBERSORT.jar -M AS_et_al_sample_avg_log2tpm.tsv -B sig_file_hugo.txt -Q > AS_et_al_LM22_panel_log2tpm_decon_result.tsv\n",
      "java -Xmx8g -Xms8g -jar CIBERSORT.jar -M AP_et_al_sample_avg_log2tpm.tsv -B sig_file_hugo.txt -Q > AP_et_al_LM22_panel_log2tpm_decon_result.tsv\n",
      "java -Xmx8g -Xms8g -jar CIBERSORT.jar -M SP_et_al_sample_avg_log2tpm.tsv -B sig_file_hugo.txt -Q > SP_et_al_LM22_panel_log2tpm_decon_result.tsv\n"
     ]
    }
   ],
   "source": [
    "for panel in ['GDSC_subtype_panal', 'LM22_panel']:\n",
    "    for sc_ds_name in ['AS_et_al', 'AP_et_al', 'SP_et_al']:\n",
    "        print (template.format(sc_ds_name, sc_ds_name, panel))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For reproducing mat_norm results (NOT IN USE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "java -Xmx8g -Xms8g -jar CIBERSORT.jar -M AS_et_al_sample_avg_mat_norm.tsv -B sig_file_hugo.txt -Q > AS_et_al_GDSC_subtype_panal_mat_norm_avg_decon_result.tsv\n",
      "java -Xmx8g -Xms8g -jar CIBERSORT.jar -M AS_et_al_sample_q95_mat_norm.tsv -B sig_file_hugo.txt -Q > AS_et_al_GDSC_subtype_panal_mat_norm_q95_decon_result.tsv\n",
      "java -Xmx8g -Xms8g -jar CIBERSORT.jar -M AS_et_al_sample_avg_mat_norm.tsv -B sig_file_hugo.txt -Q > AS_et_al_LM22_panel_mat_norm_avg_decon_result.tsv\n",
      "java -Xmx8g -Xms8g -jar CIBERSORT.jar -M AS_et_al_sample_q95_mat_norm.tsv -B sig_file_hugo.txt -Q > AS_et_al_LM22_panel_mat_norm_q95_decon_result.tsv\n"
     ]
    }
   ],
   "source": [
    "sc_ds_name = 'AS_et_al'\n",
    "\n",
    "for panel in ['GDSC_subtype_panal', 'LM22_panel']:\n",
    "\n",
    "    cmd = \"java -Xmx8g -Xms8g -jar CIBERSORT.jar -M {}_sample_avg_mat_norm.tsv -B sig_file_hugo.txt -Q > {}_{}_mat_norm_avg_decon_result.tsv\".format(sc_ds_name, sc_ds_name, panel)\n",
    "    print (cmd)\n",
    "    \n",
    "    cmd = \"java -Xmx8g -Xms8g -jar CIBERSORT.jar -M {}_sample_q95_mat_norm.tsv -B sig_file_hugo.txt -Q > {}_{}_mat_norm_q95_decon_result.tsv\".format(sc_ds_name, sc_ds_name, panel)\n",
    "    print (cmd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If there is a file path problem, then move the file to CIBERSORT.jar directory.\n",
    "\n",
    "```java -Xmx3g -Xms3g -jar CIBERSORT.jar -M AS_et_al_sample_avg.tsv -B sig_file_hugo.txt -Q ```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check if Rserve is running by ```ps ax | grep Rserve``` then kill ```JobID```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "java -Xmx3g -Xms3g -jar cibersort/CIBERSORT.jar -M data/scRNAseq_preprocessed/AS_et_al_sample_avg.tsv"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
